package com.dc.project.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.common.annotation.DataScope;
import com.dc.common.utils.PageUtil;
import com.dc.common.vo.ResultVo;
import com.dc.project.system.dao.SysUserPostDao;
import com.dc.project.system.dto.SysUserPostDto;
import com.dc.project.system.entity.SysPost;
import com.dc.project.system.entity.SysUserPost;
import com.dc.project.system.service.SysUserPostService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户岗位关联表(SysUserPost)表服务实现类
 *
 * @author zhuangcy
 * @since 2021-06-08 12:37:02
 */
@Service("sysUserPostService")
public class SysUserPostServiceImpl extends ServiceImpl<SysUserPostDao, SysUserPost> implements SysUserPostService {

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return
     */
    @Override
    public ResultVo queryById(Serializable id) {
        return ResultVo.success().data(this.getById(id));
    }

    /**
     * 分页查询用户岗位关联表
     *
     * @param dto
     * @return
     */
    @DataScope
    @Override
    public ResultVo queryByPage(SysUserPostDto dto) {
        QueryWrapper<SysUserPost> queryWrapper = new QueryWrapper<>();
        IPage<SysUserPost> page = this.page(PageUtil.convert2PageParam(dto), queryWrapper);
        return ResultVo.success().data(PageUtil.convert2PageResult(page));
    }

    /**
     * 新增用户岗位关联表
     *
     * @param sysUserPost
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo add(SysUserPost sysUserPost) {
        return ResultVo.verify(this.save(sysUserPost));
    }

    /**
     * 编辑用户岗位关联表
     *
     * @param sysUserPost
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo edit(SysUserPost sysUserPost) {
        return ResultVo.verify(this.updateById(sysUserPost));
    }

    /**
     * 根据主键删除用户岗位关联表
     *
     * @param id 主键
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo delete(Serializable id) {
        return ResultVo.verify(this.removeById(id));
    }

    /**
     * 新增用户关联岗位
     *
     * @param userId
     * @param postIds
     * @return
     */
    @Override
    public boolean addUserPosts(Integer userId, Integer[] postIds) {
        LambdaQueryWrapper<SysUserPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserPost::getUserId, userId);
        // 先删除
        boolean remove = this.remove(queryWrapper);
        // 再新增
        if (null != postIds && postIds.length != 0) {
            List<SysUserPost> userPosts = Arrays.asList(postIds).stream().map(postId -> {
                SysUserPost userPost = new SysUserPost();
                userPost.setUserId(userId);
                userPost.setPostId(postId);
                return userPost;
            }).collect(Collectors.toList());
            return this.saveBatch(userPosts, userPosts.size());
        }
        return remove;
    }

    /**
     * 判断岗位是否被使用
     *
     * @param postId
     * @return
     */
    @Override
    public boolean existUser(Integer postId) {
        return baseMapper.existUser(postId) > 0;
    }

    /**+
     * 根据用户ID查询岗位信息
     * @param userId
     * @return
     */
    @Override
    public List<SysPost> getUserPostsByUserId(Integer userId) {
        return baseMapper.getUserPostsByUserId(userId);
    }
}